#include "common.h"

//http请求信息
void http_request(char*buf, int size, char *filepath, char *host, int start)
{
    assert(buf);

    bzero(buf, size);
    snprintf(buf, size, "GET /%s HTTP/1.1\r\n"  //请求行
                        "range:bytes=%d-\r\n"   //字段range,即下载的范围
                        "host:%s\r\n\r\n", filepath, start, host);
}

//获取http头部信息
long long get_size(char *httphead)  
{
    assert(httphead);
    char *delim = "Content-Range: ";

    char *p = strstr(httphead, delim);
    if(p != NULL)
    {
        p = strstr(p, "/") + 1;
        return atoll(p);    //atoll:字符串转数字函数
    }
    return 0LL;
}

//获取http头部信息
long long get_len(char *httphead)
{
    assert(httphead);
    char *delim = "Content-Length: ";

    char *p = strstr(httphead, delim);
    if(p != NULL)
    {
        p += strlen(delim);
        return atoll(p);    //atoll:字符串转数字函数
    }
    return 0LL;
}

bool check_response(char *httphead)
{
    char *tmp = calloc(1, 128);

    //将 httphead 的第一行信息传入 tmp
    memcpy(tmp, httphead, strstr(httphead, "\r\n")-httphead);

    if(strstr(tmp, "20") && !strstr(tmp, "206"))
    {
        fprintf(stderr, "本服务器不支持断点续传，");
        fprintf(stderr, "本文件将重新下载... ...\n");
        return true;
    }
    else if(strstr(tmp, "206"))
    {
        return true;
    }

    //错误码
    if(strstr(tmp, "30"))
	{
		fprintf(stderr, "重定向错误.\n");
	}
	if(strstr(tmp, "400"))
	{
		fprintf(stderr, "请求无效.\n");
	}
	if(strstr(tmp, "401"))
	{
		fprintf(stderr, "未授权.\n");
	}
	if(strstr(tmp, "403"))
	{
		fprintf(stderr, "禁止访问.\n");
	}
	if(strstr(tmp, "404"))
	{
		fprintf(stderr, "无法找到文件.\n");
	}
	if(strstr(tmp, "405"))
	{
		fprintf(stderr, "资源被禁止.\n");
	}
	if(strstr(tmp, "407"))
	{
		fprintf(stderr, "要求代理身份验证.\n");
	}
	if(strstr(tmp, "410"))
	{
		fprintf(stderr, "永远不可用.\n");
	}
	if(strstr(tmp, "414"))
	{
		fprintf(stderr, "请求URI太长.\n");
	}
	if(strstr(tmp, "50"))
	{
		fprintf(stderr, "服务器错误.\n");
	}

    free(tmp);
    return false;
}

// void arg_parser(char *arg, char **host, char **file);
